package com.example.activity;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zykj on 2017/4/1.
 */

public class BaseActivity extends Activity {
    /**
     * 全局唯一实例
     */
    private static BaseActivity baseActivity;
    /**
     * Activity列表
     */
    private List<Activity> list  = new ArrayList<>();
    /**
     * 该类采用单例模式，不能实例化
     */
    public BaseActivity() {
    }

    /**
     * 获取类实例对象
     * @return
     */
    public static BaseActivity getInstance(){
        if (baseActivity == null){
            synchronized ("xxxx"){
                if (baseActivity == null){
                    baseActivity = new BaseActivity();
                }
            }
        }
        return baseActivity;
    }

    /**
     * 关闭所有页面
     */
    public void closeAll(){
        for (Activity activity : list){
            activity.finish();
        }
    }

    /**
     * 关闭所有页面返回到指定页面
     * @param toActivity
     */
    public void closeToActivity(Class toActivity){
        for (int i = list.size() ; i > 0 ;i-- ){
            Activity activity = list.get(i-1);
            if (activity.getClass().equals(toActivity)){
                return;
            }
            activity.finish();
            list.remove(i-1);
        }
    }

    /**
     * 保存Activity到现有列表中
     * @param activity
     */
    public void addActivity(Activity activity){
        list.add(activity);
    }
}
